<?php
/**
 * Project: Recipe
 * File: KitchenAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-26
 * Time: 下午3:43
 */


class KitchenAction extends CommonAction{

	public function index(){
		$kitchencategory = D('KitchenCategory');
		$kitchencategoryinfo = $kitchencategory->where('publish=1')->select();
//		dump($kitchencategoryinfo);
		$this->assign('category', $kitchencategoryinfo);
		$kitchenarticle = D('KitchenArticle');

		$newkitchen = $kitchenarticle->relation(true)->where('publish=1')->order('created desc')->limit(5)->select();
		$this->assign('newkitchen', $newkitchen);

		import("ORG.Util.emit_page");
		import("ORG.Util.emit_pages");
		$count = $kitchenarticle->where('recipecategoryid=70 AND publish=1')->count();
		$page = new page($count,3);
		$kitchenarticleinfo = $kitchenarticle->where('recipecategoryid=70 AND publish=1')->limit($page->setlimit())->select();
		$this->assign('kitchenarticleinfo',$kitchenarticleinfo);
		$this->assign('fpage',$page->fpage(array(5,6,7)));

		$gcount = $kitchenarticle->where('recipecategoryid=69 AND publish=1')->count();
		$gpage = new pages($gcount,3);
		$gkitchenarticleinfo = $kitchenarticle->relation(true)->where('recipecategoryid=69 AND publish=1')->limit($gpage->setlimit())->select();
		$this->assign('gkitchenarticleinfo',$gkitchenarticleinfo);
//		dump($gkitchenarticleinfo);
		$this->assign('gfpage',$gpage->fpage(array(5,6,7)));

		$this->display('tips');
	}


	function favourite(){
		if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
				if(isset($_SESSION['username']) && isset($_SESSION['userid'])){
					$favourite = M('RecipeFavourite');
					$map['founder'] = $_SESSION['userid'];
					$map['sectionid'] = $_GET['sid'];
					$map['categoryid'] = $_GET['cid'];
					$map['articleid'] = $_GET['id'];
					$favouriteinfo = $favourite->where($map)->count();
					if($favouriteinfo>0){
						$this->error('该菜谱已收藏');
					}else{
						$map['created'] = time();
						if($favourite->add($map)){
							$this->success('添加收藏成功');
						}else{
							$this->error('添加收藏失败');
						}
					}

				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function delfavourite(){
		if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
				if(isset($_SESSION['username']) && isset($_SESSION['userid'])){
					$favourite = M('RecipeFavourite');
					$map['founder'] = $_SESSION['userid'];
					$map['sectionid'] = $_GET['sid'];
					$map['categoryid'] = $_GET['cid'];
					$map['articleid'] = $_GET['id'];
					$favouriteinfo = $favourite->where($map)->count();
					if($favouriteinfo>0){
						if($favourite->where($map)->delete()){
							$this->success('删除收藏成功');
						}else{
							$this->error('删除收藏失败');
						}
					}
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function del(){
		if(isset($_GET['cid']) && !empty($_GET['cid'])){
			if(isset($_GET['id']) && !empty($_GET['id'])){
				$srcdir = './Public/Content/KitchenArticle/';

				$recipe = M('KitchenArticle');
				$kitchencomments = M('KitchenComments');

				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				$recipeinfo = $recipe->where($map)->find();

				if($recipe->where($map)->delete()){

					if(!empty($recipeinfo['image'])){
						unlink($srcdir.'Big_'.$recipeinfo['image']);
						unlink($srcdir.'Small_'.$recipeinfo['image']);
					}

					$kitchencomments->where('categoryid='.$_GET['cid'].' AND articleid='.$_GET['id'])->delete();

					$this->success('删除成功');

				}else{

					$this->error('删除失败');

				}

			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function content(){
		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$recipe = D('KitchenArticle');
				import("ORG.Util.emit_page");

				$recipe->where('recipecategoryid='.$_GET['cid'].' AND '.'id='.$_GET['id'])->setInc('downloads');

				$map['recipecategoryid'] = $_GET['cid'];
				$recipeinfo = $recipe->relation(true)->where($map)->find($_GET['id']);

				$this->assign('recipeinfo',$recipeinfo);

				if(empty($recipeinfo)){
					$this->redirect('Kitchen/index');
				}

				$return['sectionid'] = $recipeinfo['recipetypeid'];
				$return['categoryid'] = $_GET['cid'];
				$return['articleid'] = $_GET['id'];


				$user = D('User');
				$usermap['id'] = $_SESSION['userid'];
				$userinfo = $user->where($usermap)->find();
				$this->assign('userinfo',$userinfo);


				$comments = M('KitchenComments');

				$commentsmap['categoryid'] = $_GET['cid'];
				$commentsmap['articleid'] = $_GET['id'];
				$count = $comments->where($commentsmap)->count();
				$page = new page($count,3);

				$commentsinfo = $comments->limit($page->setlimit())->where($commentsmap)->order('id desc')->select();
				$avg = $comments->where($commentsmap)->avg('onlevel');
				$this->assign('levelavg',(int)$avg);
				$this->assign('commentsinfo',$commentsinfo);
				$this->assign('fpage',$page->fpage(array(5,6,7)));

				$this->display('panshi');
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function deletecomments(){

		if(isset($_GET['sid']) && !empty($_GET['sid'])){
			$comments = M('KitchenComments');
		//	dump($comments);
			if($comments->where('id='.$_GET['sid'])->delete()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function comments(){

		if(isset($_POST['categoryid']) && isset($_POST['articleid'])){
			if(!empty($_POST['categoryid']) && !empty($_POST['articleid'])){
				$comments = M('KitchenComments');
				$recipe = M('KitchenArticle');
				$recipeinfo = $recipe->where('recipecategoryid='.$_POST['categoryid'].' AND '.'id='.$_POST['articleid'])->find();
				$user = M('User');
				if(empty($_POST['code'])){
					if($commentsinfo = $comments->create()){
						$time = time();
						$commentsinfo['created'] = $time;
						if(empty($commentsinfo['username'])){
							$commentsinfo['username'] = 'Anonymous';
						}else{
							$userinfo = $user->getByUsername($commentsinfo['username']);
						}

						if($comments->add($commentsinfo)){
							if($recipeinfo['founder']!=$userinfo['id']){
								$user->where('id='.$userinfo['id'])->setInc('exp',5);
							}
							$this->success("添加添加评论成功");
						}else{
							$this->error('添加评论失败');
						}
					}else{
						$this->error($comments->getError());
					}
				}else if(md5($_POST['code'])==$_SESSION['verify']){
					if($commentsinfo = $comments->create()){
						$time = time();
						$commentsinfo['created'] = $time;
						if(empty($commentsinfo['username'])){
							$commentsinfo['username'] = 'Anonymous';
						}
						if($comments->add($commentsinfo)){
							$this->success("添加添加评论成功");
						}else{
							$this->error('添加评论失败');
						}
					}else{
						$this->error($comments->getError());
					}
				}else{
					$this->error('验证码错误');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}

	}

	function edit(){

		if(!empty($_GET['sid']) && !empty($_GET['cid']) && !empty($_GET['id'])){
			if(isset($_GET['sid']) && isset($_GET['cid']) && isset($_GET['id'])){

				$foodsection = M('FoodSection');
				$foodsectioninfo = $foodsection->field('id,name')->order('id desc')->select();
				$this->assign('foodsectioninfo', $foodsectioninfo);

				$articles = array(
					'Recipe',
					'Menu',
					'Sauce',
					'Tips & Garnishment',
				);

				$this->assign('articles', $articles);

				$setcookmethod = M('SetCookMethod');
				$setcookmethodinfo = $setcookmethod->order('id desc')->select();
				$this->assign('setcookmethodinfo', $setcookmethodinfo);

				$setcourse = M('SetCourse');
				$setcourseinfo = $setcourse->order('id desc')->select();
				$this->assign('setcourseinfo', $setcourseinfo);

				$setdietaryneeds = M('SetDietaryNeeds');
				$setdietaryneedsdinfo = $setdietaryneeds->order('id desc')->select();
				$this->assign('setdietaryneedsinfo', $setdietaryneedsdinfo);

				$setservepeople = M('SetServePeople');
				$setservepeopleinfo = $setservepeople->order('id desc')->select();
				$this->assign('setservepeopleinfo', $setservepeopleinfo);

				$seteasyhard = M('SetEasyHard');
				$seteasyhardinfo = $seteasyhard->order('id desc')->select();
				$this->assign('seteasyhardinfo', $seteasyhardinfo);

				$settotaltime = M('SetTotalTime');
				$settotaltimeinfo = $settotaltime->order('id desc')->select();
				$this->assign('settotaltimeinfo', $settotaltimeinfo);

				$setoccasion = M('SetOccasion');
				$setoccasioninfo = $setoccasion->order('id desc')->select();
				$this->assign('setoccasioninfo', $setoccasioninfo);

//				$setdeal = D('SetDeal');
//				$setdealinfo = $setdeal->relation(true)->order('id desc')->where('publish=1')->select();
//				$this->assign('setdealinfo', $setdealinfo);

				$kitchencategory = M('KitchenCategory');
				$kitchencategoryinfo = $kitchencategory->order('name')->select();
				$this->assign('kitchencategoryinfo', $kitchencategoryinfo);


				$kitchenarticle = D('KitchenArticle');
				$map['recipetypeid'] = $_GET['sid'];
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				$kitchenarticleinfo = $kitchenarticle->relation(true)->where($map)->find();
				$this->assign('recipearticleinfo', $kitchenarticleinfo);

				$this->display();

			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}


	function insert(){
		if(!empty($_POST['recipetype']) && isset($_POST['recipetype'])){
			if($_POST['recipetype']=='Kitchen' && $_POST['recipetypeid']==4){

				$tmpdir = './Public/Content/tmp/';
				$srcdir = './Public/Content/KitchenArticle/';
				$recipe = D('KitchenArticle');

				if($recipeinfo = $recipe->create()){
					$recipeinfo['founder'] = $_SESSION['userid'];

					if($recipeid = $recipe->add($recipeinfo)){

						if(!empty($recipeinfo['image'])){
							copy($tmpdir.'Big_'.$recipeinfo['image'], $srcdir.'Big_'.$recipeinfo['image']);
							copy($tmpdir.'Small_'.$recipeinfo['image'], $srcdir.'Small_'.$recipeinfo['image']);
							if($dirsrc = opendir($tmpdir)){
								while($files = readdir($dirsrc)){
									if($files!='.' && $files!='..'){
										$pathfile = $tmpdir.$files;
										if(is_file($pathfile)){
											unlink($pathfile);
										}
									}
								}
							}
						}

						$this->success("发布菜谱成功",__APP__.'/Kitchen/content/cid/'.$_POST['recipecategoryid'].'/id/'.$recipeid);
					}else{
						$this->error('发布菜谱失败');
					}
				}else{
					$this->error($recipe->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}

	}

	function update(){
		if(isset($_POST['id']) && !empty($_POST['id'])){
			$tmpdir = './Public/Content/tmp/';
			$srcdir = './Public/Content/KitchenArticle/';
			$recipe = D('KitchenArticle');

			$recipeinfos = $recipe->find($_POST['id']);
			if($recipeinfo = $recipe->create()){

				if($recipe->save()){

					if($recipeinfo['image']!=$recipeinfos['image']){

						unlink($srcdir.'Big_'.$recipeinfos['image']);
						unlink($srcdir.'Small_'.$recipeinfos['image']);

						copy($tmpdir.'Big_'.$recipeinfo['image'], $srcdir.'Big_'.$recipeinfo['image']);
						copy($tmpdir.'Small_'.$recipeinfo['image'], $srcdir.'Small_'.$recipeinfo['image']);

						if($dirsrc = opendir($tmpdir)){
							while($files = readdir($dirsrc)){
								if($files!='.' && $files!='..'){
									$pathfile = $tmpdir.$files;
									if(is_file($pathfile)){
										unlink($pathfile);
									}
								}
							}
						}
					}

					$this->success('修改菜谱成功',__APP__.'/Kitchen/content/cid/'.$recipeinfos['recipecategoryid'].'/id/'.$_POST['id']);
				}else{
					$this->error('修改菜谱失败');
				}
			}

		}else{
			$this->error('异常操作');
		}
	}

}